package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.ArrayList;
import java.util.List;

public class CollectionOperator {
    public List<Integer> getListByInterval(int left, int right) {
        //选出给定区间中所有的数字
        List<Integer> list = new ArrayList<>();
        if (left < right) {
            for (int i = left; i <= right; i++) {
                list.add(i);
            }
        } else {
            for (int i = left; i >= right; i--) {
                list.add(i);
            }
        }

        return list;
    }

    public List<Integer> getEvenListByIntervals(int left, int right) {
        //选出给定区间中所有的偶数
        List<Integer> list = new ArrayList<>();
        if (left < right) {
            for (int i = left; i <= right; i++) {
                if (i % 2 == 0) {
                    list.add(i);
                }

            }
        } else {
            for (int i = left; i >= right; i--) {
                if (i % 2 == 0) {
                    list.add(i);
                }
            }
        }

        return list;
    }

    public List<Integer> popEvenElments(int[] array) {
        //选出给定区间中所有的偶数
        List<Integer> list = new ArrayList<>();
        for (int i : array) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }

        return list;
    }

    public int popLastElment(int[] array) {
        //弹出集合最后一个元素
        return array[array.length - 1];
    }

    public List<Integer> popCommonElement(int[] firstArray, int[] secondArray) {
        //弹出两个集合的交集
        List<Integer> list = new ArrayList<>();
        for (int i : firstArray) {
            for (int it : secondArray) {
                if (i == it) {
                    list.add(i);
                }
            }
        }
        return list;
    }

    public List<Integer> addUncommonElement(Integer[] firstArray, Integer[] secondArray) {
        // 将集合二中与集合一不同的元素加入集合一
        boolean b = false;

        List<Integer> list = new ArrayList<>();
        for (Integer i : firstArray) {
            list.add(i);
        }
        for (int it : secondArray) {
            System.out.println(it);
            for (int i : firstArray) {
                if (i == it) {
                    b = true;
                }
            }
            if (b == false) {
                list.add(it);
            }
            b = false;
        }
        return list;
    }

}
